#include <iostream>
using namespace std;
class A{
private:
    A * next=NULL;
public:
    A(){}
    A(A * t){
        next = t;
    }
    virtual void fun(){
        if(next){
            next->fun();
        }
    }
};
class A1:public A{
public:
    void fun(){
        cout<<"A1"<<endl;
        this->A::fun();
    }
    A1(){}
    A1(A * t):A(t){};
};
class A2:public A{
public:
    void fun(){
        cout<<"A2"<<endl;
        this->A::fun();
    }
    A2(){}
    A2(A * t):A(t){};
};
class A3:public A{
public:
    void fun(){
        cout<<"A3"<<endl;
        this->A::fun();
    }
    A3(){}
    A3(A * t):A(t){};
};
int main() {
    A3 * a3 = new A3();
    A2 * a2 = new A2(a3);
    A * pa = new A1(a2);
    pa->fun();
}